﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using ConsoleAppBanco.Dominio;

namespace ConsoleAppBanco
{
    class Program
    {
        static void Main(string[] args)
        {

            string ticket = "";
            Console.WriteLine("Ingrese su número de ticket:");
            ticket = Console.ReadLine();

            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create("http://localhost:1255/ServiceEntidad/WFCServiceREST/WCFPagosService.svc/Operacion/" + ticket);



            req2.Method = "GET";
            HttpWebResponse res2 = null;
            try
            {
                //OPERACION
                res2 = (HttpWebResponse)req2.GetResponse();
                StreamReader reader2 = new StreamReader(res2.GetResponseStream());
                string operacionJson2 = reader2.ReadToEnd();
                JavaScriptSerializer js2 = new JavaScriptSerializer();
                OperacionLinea operacionObtenido = js2.Deserialize<OperacionLinea>(operacionJson2);
                //Assert.AreEqual("7", operacionObtenido.idEstado);
                //Assert.AreEqual("DemoPrueba", operacionObtenido.descripcion);

                //POST Pago
                string idOp = operacionObtenido.idOperacion.ToString();

                Console.WriteLine("Operación: {0}", idOp);
                Console.WriteLine("-----------------------");

                string postdata = "{\"idOperacion\":\"" + idOp + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:1255/ServiceEntidad/WFCServiceREST/WCFPagosService.svc/Pagos");
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                HttpWebResponse res = null;

                res = (HttpWebResponse)req.GetResponse();
                StreamReader reader3 = new StreamReader(res.GetResponseStream());
                string pagoJson3 = reader3.ReadToEnd();
                JavaScriptSerializer js3 = new JavaScriptSerializer();
                Pago pagoObtenido = js3.Deserialize<Pago>(pagoJson3);

                Console.WriteLine("Pago: {0}", pagoObtenido.idPago.ToString());
                Console.WriteLine("Estado: {0}", pagoObtenido.estado.ToString());
                Console.WriteLine("Modo: {0}", pagoObtenido.modo.ToString());
                Console.WriteLine("Entidad Bancaria: {0}", pagoObtenido.entidadBancaria.ToString());

                //EVENTO

                HttpWebRequest reqEvento = (HttpWebRequest)WebRequest.Create("http://localhost:1255/ServiceEntidad/WFCServiceREST/WCFEventoService.svc/Eventos/Operacion/" + idOp);



                reqEvento.Method = "GET";
                HttpWebResponse resEvento = null;
                resEvento = (HttpWebResponse)reqEvento.GetResponse();
                StreamReader readerEvento = new StreamReader(resEvento.GetResponseStream());
                string eventoJson = readerEvento.ReadToEnd();
                JavaScriptSerializer jsEvento = new JavaScriptSerializer();
                EventoDeportivo eventoObtenido = jsEvento.Deserialize<EventoDeportivo>(eventoJson);

                Console.WriteLine("Evento: {0}", eventoObtenido.idEvento.ToString());
                //CLIENTE

                string idCliente = operacionObtenido.idCliente.ToString();
                HttpWebRequest reqCliente = (HttpWebRequest)WebRequest.Create("http://localhost:1255/ServiceEntidad/WFCServiceREST/WCFClientesService.svc/Clientes/ID/" + idCliente);



                reqCliente.Method = "GET";
                HttpWebResponse resCliente = null;
                resCliente = (HttpWebResponse)reqCliente.GetResponse();
                StreamReader readerCliente = new StreamReader(resCliente.GetResponseStream());
                string clienteJson = readerCliente.ReadToEnd();
                JavaScriptSerializer jsCliente = new JavaScriptSerializer();
                Cliente clienteObtenido = jsCliente.Deserialize<Cliente>(clienteJson);

                Console.WriteLine("Cliente: {0}", clienteObtenido.idcliente.ToString());
                //E-MAIL desde UUID
                string _uuid = clienteObtenido.uuid;
                HttpWebRequest reqEmail = (HttpWebRequest)WebRequest.Create("http://localhost:8090/ServicioRESTPortal-portlet/rest/Usuarios/" + _uuid);



                reqEmail.Method = "GET";
                HttpWebResponse resEmail = null;
                resEmail = (HttpWebResponse)reqEmail.GetResponse();
                StreamReader readerEmail = new StreamReader(resEmail.GetResponseStream());
                string emailJson = readerEmail.ReadToEnd();
                JavaScriptSerializer jsEmail = new JavaScriptSerializer();
                string emailObtenido = jsEmail.Deserialize<string>(emailJson);

                //EnviarMensajeCola();
                const string quote = "\"";
                string _cadMensaje = "{userPortal="+clienteObtenido.uuid+", tipo=compra, MQCorreoConfirmacionCompra={" 
                    + quote + "operacionEnLinea" + quote + ":{" + quote + "idOperacion" + quote + ":"+operacionObtenido.idOperacion.ToString()
                    +"," + quote + "idCliente" + quote + ":"+clienteObtenido.idcliente.ToString()+"," + quote + "montoTotal" + quote + ":"
                    +operacionObtenido.montoTotal.ToString()+"," + quote + "fechaOperacion" + quote + ":" + quote + operacionObtenido.fechaOperacion 
                    + quote + "," + quote + "__hashCodeCalc" + quote + ":false}," + quote + "pago" + quote + ":{" + quote + "idPago" + quote 
                    + ":"+pagoObtenido.idPago.ToString()+"," + quote + "fechaRegistro" + quote + ":" + quote +pagoObtenido.fechaRegistro + quote 
                    + "," + quote + "estado" + quote + ":"+pagoObtenido.estado.ToString()+"," + quote + "modo" + quote + ":"+pagoObtenido.modo.ToString()+"," 
                    + quote + "entidadBancaria" + quote + ":" + quote + pagoObtenido.entidadBancaria + quote + "," + quote + "numeroTarjeta" + quote + ":" + quote 
                    + pagoObtenido.numeroTarjeta + quote + "," + quote + "idOperacion" + quote + ":"+operacionObtenido.idOperacion.ToString()+"," + quote + "__hashCodeCalc" + quote 
                    + ":false}," + quote + "eventoDeportivo" + quote + ":{"+ quote +"idEvento"+ quote +":"+eventoObtenido.idEvento.ToString()+","+ quote +"fechaInicio"+ quote 
                    +":"+ quote +eventoObtenido.fechaInicio+ quote +","+ quote +"fechaFinEvent"+ quote +":"+ quote +eventoObtenido.fechaFinEvent+ quote 
                    +","+ quote +"descripcion"+ quote +":"+ quote +eventoObtenido.descripcion+ quote +","+ quote +"idEstadio"+ quote +":"+eventoObtenido.idEstadio.ToString()+","+ quote 
                    +"uuidContenido"+ quote +":"+ quote +eventoObtenido.uuidContenido+ quote +","+ quote +"limiteEntradas"+ quote +":"+eventoObtenido.limiteEntradas.ToString()+","+ quote +"precioEntrada"
                    + quote +":"+eventoObtenido.precioEntrada.ToString()+","+ quote +"fechaRegistro"+ quote +":"+ quote +eventoObtenido.fechaRegistro+ quote +","+ quote +"__hashCodeCalc"
                    + quote + ":false}," + quote + "__hashCodeCalc" + quote + ":false}, emailuser=" + emailObtenido + "}";
               
                demo(_cadMensaje);

                //string _cadMensaje = "{userPortal=268b367a-8b20-46dd-975b-7714567ae702, tipo=compra, MQCorreoConfirmacionCompra={" + quote + "operacionEnLinea" + quote + ":{" + quote + "idOperacion" + quote + ":6," + quote + "idCliente" + quote + ":7," + quote + "montoTotal" + quote + ":68.0," + quote + "fechaOperacion" + quote + ":" + quote + "2014-04-11 01:14:22Z" + quote + "," + quote + "__hashCodeCalc" + quote + ":false}," + quote + "pago" + quote + ":{" + quote + "idPago" + quote + ":6," + quote + "fechaRegistro" + quote + ":" + quote + "2014-04-11 01:14:22Z" + quote + "," + quote + "estado" + quote + ":2," + quote + "modo" + quote + ":2," + quote + "entidadBancaria" + quote + ":" + quote + "Interbank" + quote + "," + quote + "numeroTarjeta" + quote + ":" + quote + "001-268-212" + quote + "," + quote + "idOperacion" + quote + ":6," + quote + "__hashCodeCalc" + quote + ":false}," + quote + "eventoDeportivo" + quote + "";
                //:{"idEvento":1,"fechaInicio":"2014-04-13 08:00:00","fechaFinEvent":"2014-04-13 11:00:00","descripcion":"Descentralizado","idEstadio":1,"uuidContenido":"1","limiteEntradas":234,"precioEntrada":34.0,"fechaRegistro":"2014-03-13 00:00:00","__hashCodeCalc":false},"__hashCodeCalc":false}, emailuser=danieldelgado20g@gmail.com}";

                //Fin

                Console.ReadLine();
            }
            catch (WebException e)
            {
                HttpStatusCode code = ((HttpWebResponse)e.Response).StatusCode;
                string message = ((HttpWebResponse)e.Response).StatusDescription;
                StreamReader reader = new StreamReader(e.Response.GetResponseStream());
                string error = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                string mensaje = js.Deserialize<string>(error);
                //Assert.AreEqual("Error en Obtener Estado", mensaje);
            }
        }

        private static void demo(string cadenaMensaje)
        {

            

            //string postdata = "{\"body\":\"DemoPruebaMnesaje\"}";
            string postdata = "body=" + cadenaMensaje;
            byte[] data = Encoding.UTF8.GetBytes(postdata);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8161/api/message/confirmar/hola?type=queue");
            req.Method = "POST";
            req.ContentLength = data.Length;
            //   req.ContentType = "application/json";
            req.ContentType = "application/x-www-form-urlencoded";
            String username = "admin";
            String password = "admin";
            String encoded = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            req.Headers.Add("Authorization", "Basic " + encoded);
            var reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            HttpWebResponse res = null;

            res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string asientoJson = reader.ReadToEnd();

            Console.WriteLine(asientoJson);
            Console.ReadLine();

            //Assert.AreEqual("Message sent", asientoJson);

            //Servicio Cliente , esta en operacion en linea el atributo idCliente

            //Servicio Evento

        }



    }
}
